{extend name="base"/}
{block name="resources"}
<style>
	.layui-layout-admin.admin-style-2 .layui-body .layui-tab + .body-content{padding-top: 65px !important;}
	.promote-flex {display:flex;margin-top:15px;padding:0 10px 0;}
	.promote-img {background-color:#f8f8f8;display:flex;justify-content:center;align-items:center;width:160px;height:160px;}
	.promote-img img {width:140px;height:140px;}
	.label-width {width:100px;}
	.block-left {margin-left:100px !important;display:flex;}
	.promote-download {margin-left:20px;}
	.path-wrap {margin-left:20px;}
	.path-wrap .copy {width:1px;height:0px;border:0px;}
	.path-wrap span {display:block;margin-bottom:10px;}
	.path-wrap p input {height:30px;line-height:30px;border:1px solid #e6e6e5;width:260px;padding:0px 5px;border-radius:5px;margin-right:10px;}
</style>
{/block}
{block name="main"}

<!-- 筛选面板 -->
<!-- <div class="screen layui-collapse">
	<div class="layui-colla-item">
		<form class="layui-colla-content layui-form layui-show">
			<div class="layui-form-item">

				<div class="layui-inline">
					<label class="layui-form-label">接收范围：</label>
					<div class="layui-input-inline">
						<select name="receiving_type">
							<option value="">全部</option>
							<option value="mobile">手机端</option>
						</select>
					</div>
				</div>

				<button class="layui-btn" lay-submit lay-filter="search">筛选</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>

		</form>
	</div>
</div> -->

<!-- 搜索框 -->
<div class="single-filter-box">
	<button class="layui-btn" onclick="add()">添加公告</button>
	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="search_text" placeholder="请输入公告名称" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<!-- 列表 -->
<table id="notice_list" lay-filter="notice_list"></table>

<!-- 操作 -->
<script type="text/html" id="operation">
	<div class="table-btn">
		<a class="layui-btn" lay-event="promote">推广</a>
		<a class="layui-btn" lay-event="basic">查看</a>
		<a class="layui-btn" lay-event="edit">编辑</a>
		{{# if(!d.is_top){ }}
			<a class="layui-btn" lay-event="setTop">置顶</a>
		{{# } }}
		<a class="layui-btn" lay-event="del">删除</a>
	</div>
</script>

<!-- 编辑排序 -->
<script type="text/html" id="editSort">
	<input name="sort" type="number" onchange="editSort({{d.id}}, this)" value="{{d.sort}}" class="layui-input edit-sort len-short">
</script>

<!-- 推广 -->
<script type="text/html" id="promote">
	<div class="promote-flex">
		<div class="promote-img">
			<img src="{{ ns.img(d.h5.path) }}" alt="推广二维码">
		</div>
		<div class="layui-form">
			<div class="layui-form-item">
				<label class="layui-form-label label-width">选择渠道：</label>
				<div class="layui-input-block block-left">
					<input type="radio" name="promote_type" value="h5" title="H5" checked lay-filter="promote_type" >
					<input type="radio" name="promote_type" value="weapp" title="微信小程序" lay-filter="promote_type">
					{if $pc_is_exit == 1}
					<input type="radio" name="promote_type" value="pc" title="PC" lay-filter="promote_type">
					{/if}
					{if addon_is_exit('aliapp', request()->siteid())}
					<input type="radio" name="promote_type" value="aliapp" title="支付宝小程序" lay-filter="promote_type">
					{/if}
				</div>
			</div>
			<div class="path-wrap">
				<span>推广链接</span>
				<p class="tips text-color">
					<input type="text" value="{{d.h5.url}}" readonly />
					<a class="layui-btn" href="javascript:ns.copy('article_url_{{ d.id }}');">复制</a>
				</p>
				<input type="text" id="article_url_{{ d.id }}" value="{{d.h5.url}}" readonly class="copy"/>
			</div>
			<div class="promote-download">
				<a class="text-color" href="{{ ns.img(d.h5.path) }}" download>下载二维码</a>
			</div>
		</div>
	</div>
</script>
{/block}
{block name="script"}
<script type="text/javascript">
	var form,laytpl,table,repeat_flag = false; //防重复标识
	layui.use(['form','laytpl'], function() {
		form = layui.form;
		laytpl = layui.laytpl;
		form.render();

		table = new Table({
			elem: '#notice_list',
			url: ns.url("shop/notice/index"),
			cols: [
				[{
					width: '35%',
					title: '公告标题',
					unresize: 'false',
					templet: function(data) {
						var html = data.is_top ? '<span class="required">[ 置顶 ] </span>' : '';
						html += data.title;
						return html;
					}
				},
				// 	{
				// 	field:'receiving_name',
				// 	width: '20%',
				// 	title: '接收范围',
				// 	unresize: 'false',
				// },
					{
					width: '25%',
					title: '创建时间',
					unresize: 'false',
					templet: function(data) {
						return ns.time_to_date(data.create_time);
					}
				}, {
                    field: 'sort',
                    unresize:'false',
                    title: '排序',
                    sort : true,
                    width: '20%',
                    templet: '#editSort'
                }, {
					title: '操作',
					unresize: 'false',
					templet: '#operation',
                    align : 'right'
				}
			]]
		});

		table.tool(function(obj) {
			var data = obj.data;
			var event = obj.event;
			switch (obj.event) {
				case 'promote': // 推广
					promote(data);
					break;
				case 'edit': //编辑
					location.href = ns.url("shop/notice/editNotice?id=" + data.id);
					break;
				case 'del': //删除
					deleteNotice(data.id);
					break;
				case 'setTop': // 置顶
					modifySiteNoticeTop(data.id);
					break;
				case 'basic':
					location.href = ns.url("shop/notice/detail?id=" + data.id);
					break;
			}
		});
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});

		function deleteNotice(id) {

			if (repeat_flag) return;
			repeat_flag = true;
			layer.confirm('确定要删除该公告吗?', function() {
				$.ajax({
					type: "post",
					async: false,
					url: ns.url("shop/notice/deleteNotice"),
					data: {
						'id': id
					},
					dataType: "JSON",
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;

						if (res.code == 0) {
							table.reload();
						}
					}
				});
			}, function() {
					repeat_flag = false;
					layer.close();
				}
			);
		}
        table.on("sort",function (obj) {
            table.reload({
                page: {
                    curr: 1
                },
                where: {
                    order:obj.field,
                    sort:obj.type
                }
            });
        });

		function modifySiteNoticeTop(id) {
			if (repeat_flag) return;
			repeat_flag = true;
			
			$.ajax({
				type: "POST",
				dataType: "JSON",
				async: false,
				url: ns.url("shop/notice/modifyNoticeTop"),
				data: {id},
				success: function(res) {
					layer.msg(res.message);
					repeat_flag = false;
					if (res.code == 0) {
						table.reload();
					}
				}
			});
		}
	});
	
	function add() {
		location.href = ns.url("shop/notice/addNotice");
	}

    // 监听单元格编辑
    function editSort(id, event){
        var data = $(event).val();

        if (data == '') {
            $(event).val(0);
            data = 0;
        }

        if(!new RegExp("^-?[0-9]\\d*$").test(data)){
            layer.msg("排序号只能是整数");
            return ;
        }
        if(data<0){
            layer.msg("排序号必须大于0");
            return ;
        }
        $.ajax({
            type: 'POST',
            url: ns.url("shop/notice/modifySort"),
            data: {
                sort: data,
                id: id
            },
            dataType: 'JSON',
            success: function(res) {
                layer.msg(res.message);
                if(res.code==0){
                    table.reload();
                }
            }
        });
    }

	function promote(data) {
		if (repeat_flag) return false;
		repeat_flag = true;
		$.ajax({
			type: "POST",
			url: ns.url("shop/notice/promote"),
			data: {
				notice_id: data.id
			},
			dataType: 'JSON',
			success: function (res) {
				if(res.code==0){
					res.data.id = data.id;
					laytpl($("#promote").html()).render(res.data, function (html) {
						layer.open({
							type: 1,
							area: ['700px', '300px'],
							offset: '155px',
							title: ['推广'],
							content: html,
							success: function(){
								// 推广渠道监听
								form.on('radio(promote_type)', function(radio){

									if(radio.value == 'h5' || radio.value == 'pc') {
										var content = "";
										content += '<img src="'+ ns.img(res.data[radio.value].path) +'"/>';
										$('.promote-img').html(content);
										$('.path-wrap p input').val(res.data[radio.value].url);
										$('.path-wrap .copy').val(res.data[radio.value].url);
										$('.promote-download a').attr('href',res.data[radio.value].path).show();
										$(".path-wrap").show();
									}else {
										if(res.data[radio.value] && res.data[radio.value].path){
											$('.promote-img img').attr('src', ns.img(res.data[radio.value].path));
											$('.promote-download a').attr('href',ns.img(res.data[radio.value].path)).show();
										}else{
											var content = "";
											content += '<span>小程序配置错误</span>';
											$('.promote-img').html(content);
											$('.promote-download a').attr('href', '').hide();
										}
										$(".path-wrap").hide();
									}
								});
							}
						});
						form.render();
						repeat_flag = false;
					});

				}else{
					layer.msg('加载失败，请重试');
					repeat_flag = false;
				}
			}
		});

	}
</script>
{/block}